#!/bin/bash

# 打包txt文件中的镜像列表，每个镜像一个tar包

IMAGE_LIST="images.txt"

TEMP_DIR="docker_images"
mkdir -p "$TEMP_DIR"

while IFS= read -r image; do
    # 根据需要选择是否要在线拉取
    # docker pull $image
    IMAGE_FILE="${TEMP_DIR}/$(echo $image | tr '/:' '_').tar"
    echo "Saving image $image to $IMAGE_FILE"
    docker save -o "$IMAGE_FILE" "$image"
done < "$IMAGE_LIST"

TAR_FILE="docker_images.tar"
echo "Creating final tar package $TAR_FILE"
tar -cvf "$TAR_FILE" -C "$TEMP_DIR" .

rm -rf "$TEMP_DIR"

echo "All images have been saved to $TAR_FILE."
